//
//  InputTextView.m
//  MyChat
//
//  Created by Ansen on 15-2-17.
//  Copyright (c) 2015年 Ansen. All rights reserved.
//

#import "InputTextView.h"
#import "UIImage+Fit.h"
#import "EmoteSelectorView.h"

@interface InputTextView ()<EmoteSelectorViewDelegate>
{
    EmoteSelectorView *_emoteView;
}

@property (nonatomic, weak) IBOutlet UITextField *inuputText;

@property (nonatomic, weak) IBOutlet UIButton *recoderBtn;

@property (nonatomic, weak) IBOutlet UIButton *voiceBtn;

@property (nonatomic, weak) IBOutlet UIButton *emoteBtn;


@end

@implementation InputTextView

-(void)awakeFromNib
{
    [_recoderBtn setBackgroundImage:[UIImage resizeImage:@"VoiceBtn_Black"] forState:UIControlStateNormal];
    [_recoderBtn setBackgroundImage:[UIImage resizeImage:@"VoiceBtn_BlackHL"] forState:UIControlStateHighlighted];
    
    _emoteView = [[EmoteSelectorView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
    _emoteView.delegate = self;
}

#pragma mark 设置按钮的图像
-(void)setButton:(UIButton *)button imageName:(NSString *)imgName imageHLName:(NSString *)imageHLName
{
    UIImage *image = [UIImage imageNamed:imgName];
    UIImage *imageHL = [UIImage imageNamed:imageHLName];
    
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:imageHL forState:UIControlStateHighlighted];
    
}

#pragma mark - Actions
#pragma mark 点击声音切换按钮
-(IBAction)clickVoice:(UIButton *)btn{
  
    btn.tag = !btn.tag;
    
    // 2 隐藏输入文本框
    _inuputText.hidden = btn.tag;
    // 3 显示录音按钮
    _recoderBtn.hidden = !btn.tag;
    
    if (btn.tag) {
        // 1) 关闭键盘
        [_inuputText resignFirstResponder];

        // 2) 切换按钮图标，显示键盘图标
        [self setButton:btn imageName:@"ToolViewInputText" imageHLName:@"ToolViewInputTextHL"];
        
    }else{
        
        // 打开文本录入
        // 1) 切换按钮图标，显示录音图标
        [self setButton:btn imageName:@"ToolViewInputVoice" imageHLName:@"ToolViewInputVoiceHL"];
        
        // 2) 打开键盘
        [_inuputText becomeFirstResponder];
        
        // 3) 关闭表情
        [self clickEmote:_emoteBtn];
    }
}

#pragma mark 点击表情切换按钮
-(IBAction)clickEmote:(UIButton *)btn{
    
    // 1. 如果当前正在录音，需要切换到文本状态
    if (!_recoderBtn.hidden) {
        [self clickVoice:_voiceBtn];
    }
    
    // 2) 激活键盘
    [_inuputText becomeFirstResponder];
    
    // 2. 判断当前按钮的状态，如果是输入文本，替换输入视图(选择表情)
    // 1) 设置按钮的tag
    btn.tag = !btn.tag;
    
    if (btn.tag) {
        // 显示表情选择视图
        [_inuputText setInputView:_emoteView];
        // 切换按钮图标，显示键盘选择图像
        [self setButton:btn imageName:@"ToolViewInputText" imageHLName:@"ToolViewInputTextHL"];
    }else{
        
        // 显示系统默认键盘
        [_inuputText setInputView:nil];
        
        // 切换按钮图标，显示表情选择图像
        [self setButton:btn imageName:@"ToolViewEmotion" imageHLName:@"ToolViewEmotionHL"];
    }
    
    // 3. 刷新键盘的输入视图
    [_inuputText reloadInputViews];
}

#pragma mark - 表情选择视图代理方法
// 拼接表情字符串
-(void)emoteSelectorViewSelecteString:(NSString *)emote
{
    // 拼接现有文本
    // 1. 取出文本
    NSMutableString *strM = [NSMutableString stringWithString:_inuputText.text];
    
    // 2. 拼接字符串
    [strM appendString:emote];
    
     // 3. 设置文本
    _inuputText.text = strM;
    
}

// 删除字符串
-(void)emoteSelectViewRemoverChar
{
    if (_inuputText.text.length != 0) {
        NSString *str = _inuputText.text;
        _inuputText.text = [str substringToIndex:(str.length -1)];
    }

}


@end
